package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotSensorService;
import com.lechi.web.system.domain.IotSensorBo;
import com.lechi.web.system.mapper.iot.IotSensorMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 设备观测要素Service业务层处理
 *
 * @author yangguang
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IotSensorServiceImpl implements IIotSensorService {

    private final IotSensorMapper iotSensorMapper;

    /**
     * 查询设备观测要素
     *
     * @param ids 设备观测要素主键
     * @return 设备观测要素
     */
    @Override
    public IotSensorBo selectIotSensorById(Long[] ids) {
        try {
            return iotSensorMapper.selectIotSensorById(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询设备观测要素", method, ids));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 查询设备观测要素列表
     *
     * @param iotSensorBo 设备观测要素
     * @return 设备观测要素
     */
    @Override
    public List<IotSensorBo> selectIotSensorList(IotSensorBo iotSensorBo) {
        try {
            return iotSensorMapper.selectIotSensorList(iotSensorBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询设备观测要素列表", method, iotSensorBo));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增设备观测要素
     *
     * @param iotSensorBo 设备观测要素
     */
    @Override
    public void insertIotSensor(IotSensorBo iotSensorBo) {
        iotSensorBo.setCreateTime(new Date());
        try {
            iotSensorMapper.insertIotSensor(iotSensorBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增设备观测要素", method, iotSensorBo));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改设备观测要素
     *
     * @param iotSensorBo 设备观测要素
     */
    @Override
    public void updateIotSensor(IotSensorBo iotSensorBo) {
        try {
            iotSensorMapper.updateIotSensor(iotSensorBo);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改设备观测要素", method, iotSensorBo));
            throw new ServiceException(ErrorCodeEnum.UPDATE_ERROR.getValue(), ErrorCodeEnum.UPDATE_ERROR.getKey());
        }
    }

    /**
     * 批量删除设备观测要素
     *
     * @param ids 需要删除的设备观测要素主键
     */
    @Override
    public void deleteIotSensorByIds(Long[] ids) {
        try {
            iotSensorMapper.deleteIotSensorByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量删除设备观测要素", method, ids));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }
}
